package com.lemi.util;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;

@Component
public class BspUtils {

	/**
	 * 获取请求ip
	 * @param request
	 * @return
	 */
    public static String getIpAddress(HttpServletRequest request) {  
        String ip = request.getHeader("x-forwarded-for");  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_CLIENT_IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;  
    }

	static DecimalFormat df=new DecimalFormat("0");

	public static Object getValue(Cell cell) {

		if (null == cell) {
			return "";
		}
		Object value = null;
		System.out.println(cell.toString()+"===="+cell.getCellType());
		switch (cell.getCellType()) {
			case BOOLEAN:
				value = cell.getBooleanCellValue();
				break;
			case NUMERIC:
				// 日期类型，转换为日期
				if (DateUtil.isCellDateFormatted(cell)) {
					value = cell.getDateCellValue();
				} else {// 数值类型
//					value = df.format(cell.getNumericCellValue());
					cell.setCellType(CellType.STRING);
					value = cell.getStringCellValue();
				}
				break;
			default:
				value = cell.toString();
				break;
		}
		return value;
	}
    
    /**
     * 去除多余英文逗号
     * @param fms
     * @return
     */
	public static String removeComma(String fms) {
		if (fms != null) {
			fms = fms.trim().replaceAll(",{2,}", ",");
			if (fms.startsWith(",")) {
				fms = fms.substring(1);

			}
			if (fms.endsWith(",")) {
				fms = fms.substring(0, fms.length() - 1);
			}
		} else {
			fms = "";
		}
		return fms;
	}
	
	/**
	 * 获取消耗时间
	 * @param before
	 * @param after
	 * @return
	 */
	public static String getConsumeTime(Date before,Date after){
		if(before!=null && after!=null){
			Long shift = after.getTime()-before.getTime();
			if(shift<86400000){
				long min = shift%60000==0?(shift/60000):(shift/60000 + 1);
				if(shift<3600000){//一小时内
					return "耗时" + (min) + "分钟";
				}else{//一天内
					return "耗时"+(min/60) +"小时" + (min%60) + "分钟";
				}
			}else{
				long hour = shift%3600000==0?(shift/3600000):(shift/3600000+1);
				return "耗时"+(hour/24) +"天" + (hour%24) + "小时";
			}
		}
		return "";
	}

	/**
	 * 获取0点时间
	 * @param date
	 * @param shift
	 * @return
	 */
	public static Date getDayTime(Date date, Integer shift){
		Calendar instance = Calendar.getInstance();
		if(date!=null) {
			instance.setTime(date);
		}
		instance.add(Calendar.DATE, shift);
		instance.set(Calendar.HOUR_OF_DAY, 0);
		instance.set(Calendar.MINUTE, 0);
		instance.set(Calendar.SECOND, 0);
		instance.set(Calendar.MILLISECOND, 0);
		return instance.getTime();
	}
	
	public static String getSPro(int iPro) {
		String sPro = "";
		switch (iPro) {
		case 1:
			sPro = "上海";
			break;
		case 2:
			sPro = "云南";
			break;
		case 3:
			sPro = "内蒙古";
			break;
		case 4:
			sPro = "北京";
			break;
		case 5:
			sPro = "吉林";
			break;
		case 6:
			sPro = "四川";
			break;
		case 7:
			sPro = "天津";
			break;
		case 8:
			sPro = "宁夏";
			break;
		case 9:
			sPro = "安徽";
			break;
		case 10:
			sPro = "山东";
			break;
		case 11:
			sPro = "山西";
			break;
		case 12:
			sPro = "广东";
			break;
		case 13:
			sPro = "广西";
			break;
		case 14:
			sPro = "新疆";
			break;
		case 15:
			sPro = "江苏";
			break;
		case 16:
			sPro = "江西";
			break;
		case 17:
			sPro = "河北";
			break;
		case 18:
			sPro = "河南";
			break;
		case 19:
			sPro = "浙江";
			break;
		case 20:
			sPro = "海南";
			break;
		case 21:
			sPro = "湖北";
			break;
		case 22:
			sPro = "湖南";
			break;
		case 23:
			sPro = "甘肃";
			break;
		case 24:
			sPro = "福建";
			break;
		case 25:
			sPro = "西藏";
			break;
		case 26:
			sPro = "贵州";
			break;
		case 27:
			sPro = "辽宁";
			break;
		case 28:
			sPro = "重庆";
			break;
		case 29:
			sPro = "陕西";
			break;
		case 30:
			sPro = "青海";
			break;
		case 31:
			sPro = "黑龙江";
			break;
		}
		return sPro;
	}

	public static void main(String[] args) {

	}
}
